﻿using System;
using System.Collections.Generic;
using BLWCMS.Core.Helpers;
using BLWCMS.Core.IBusiness;
using BLWCMS.Core.Models;
using Nancy;
using Nancy.Session;
using Simple.Data;

namespace BLWCMS.Core.Business
{
    public class LoginBusiness:ILoginBusiness
    {
        public string LoginIn(Models.LoginModel loginModel,string oCheckCode)
        {
            if (loginModel.CheckCode.ToUpper() != oCheckCode.ToUpper())
            {
                return ResCode.N500("验证码不正确！");
            }
            else
            {
                try
                {
                    var db = Database.OpenConnection(Dbcontext.ConnectionString);
                    List<AdminModel> adminModels = db.BL_Admins.FindAllByAccountAndPassword(loginModel.Account,
                        StringHelper.PasswordEncryption(loginModel.Password)).ToList<AdminModel>();
                    if (adminModels.Count==1)
                    {
                        foreach (AdminModel adminModel in adminModels)
                        {
                            return adminModel.Id.ToString();
                        }
                        return null;
                    }
                    else
                    {
                        return ResCode.N500("账号或密码不正确！");
                    }
                }
                catch (Exception e)
                {
                    return ResCode.N500(e.Message);
                }
            }
        }
    }
}
